package org.aeon.client.view.components.sessionPanel;

import org.aeon.client.context.GlobalContext;
import org.aeon.client.view.ApplicationUI;
import org.aeon.client.view.components.button.OperationButton;
import org.aeon.client.view.components.sessionPanel.confirmDialog.DeleteFriendConfirmDialog;
import org.aeon.entity.dto.UserInfoDto;
import org.aeon.message.request.FriendDeleteRequestMessage;
import org.aeon.utils.SimpleRequestUtil;
import org.aeon.utils.SwingUtil;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.util.Date;
import java.util.Objects;

/**
 * @author: MissingAeon
 * @date: 2024/2/20 14:05
 * @description:
 */
public class UserInfoPanel extends JPanel {
    private final SessionPanel sessionPanel;
    private JLabel emailLabel;
    private JLabel nicknameLabel;
    private JLabel usernameLabel;
    private JLabel label;
    private OperationButton operationBut;
    private OperationButton sendBut;

    public UserInfoPanel(SessionPanel sessionPanel) {
        this.sessionPanel = sessionPanel;

        this.setLayout(new GridBagLayout());

        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridheight = 15;
        constraints.insets = new Insets(60, 50, 1, 50);
        this.add(infoPanel1(), constraints);
        constraints.gridx = 0;
        constraints.gridy = 16;
        constraints.gridheight = 30;
        constraints.insets = new Insets(5, 50, 1, 50);
        this.add(infoPanel2(), constraints);
        constraints.gridx = 0;
        constraints.gridy = 46;
        constraints.gridheight = 7;
        constraints.insets = new Insets(5, 50, 60, 50);
        this.add(operationPanel(), constraints);

    }

    private JPanel infoPanel1() {
        final JPanel panel = createPanel(new Dimension(600, 150));
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        // 昵称
        nicknameLabel = new JLabel("昵称");
        nicknameLabel.setFont(new Font("黑体", Font.BOLD, 26));
        panel.add(nicknameLabel);
        // 用户名
        usernameLabel = new JLabel("用户名");
        usernameLabel.setFont(new Font("黑体", Font.BOLD, 13));
        panel.add(usernameLabel);
        // 在线情况
        label = new JLabel("在线");
        label.setFont(new Font("黑体", Font.BOLD, 16));
        panel.add(label);
        return panel;
    }

    private JPanel infoPanel2() {
        final JPanel panel = createPanel(new Dimension(600, 300));
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        // 等级
        panel.add(new JLabel("等级"));
        // 邮箱
        emailLabel = new JLabel("邮箱");
        emailLabel.setFont(new Font("黑体", Font.BOLD, 18));
        panel.add(emailLabel);
        return panel;
    }


    private JPanel operationPanel() {
        // 操作
        final JPanel panel = createPanel(new Dimension(600, 70));
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

        panel.add(Box.createHorizontalGlue());

        operationBut = new OperationButton();
        panel.add(operationBut);

        panel.add(Box.createHorizontalStrut(60));
        // 发送消息
        sendBut = new OperationButton("发送消息", new Color(0xFF6696A9, true));
        panel.add(sendBut);

        panel.add(Box.createHorizontalGlue());

        return panel;
    }

    public void fillInData(UserInfoDto user) {
        nicknameLabel.setText(user.getNickname());
        usernameLabel.setText(user.getUsername());
        emailLabel.setText(user.getEmail());

        // 添加|删除 好友
        operationBut.setEnabled(true);
        final GlobalContext context = GlobalContext.getContext();
        if (user.getRelation() != null && user.getRelation()) {
            operationBut.setBackground(new Color(0xFFAF3939, true));
            operationBut.setText("删除好友");
            operationBut.addActionListener(e -> {
                final ApplicationUI mainUI = context.getMainUI();
                final JPanel mask = SwingUtil.createMask(mainUI.getWidth(), mainUI.getHeight() - 30);
                final DeleteFriendConfirmDialog dialog = new DeleteFriendConfirmDialog();
                dialog.submit(event -> {
                    mainUI.removePopUpLayer(mask);
                    if (dialog.isCheckTheBox()) {
                        // TODO 黑名单添加
                    } else {
                        final FriendDeleteRequestMessage reqMsg = new FriendDeleteRequestMessage();
                        reqMsg.setMessage(context.getCurrentUser().getId(), user.getId());
                        SimpleRequestUtil.send(context, reqMsg);
                    }
                });
                dialog.cancel(event -> mainUI.removePopUpLayer(mask));

                mask.add(dialog);
                mainUI.addPopUpLayer(mask);
            });
        } else {
            operationBut.setBackground(new Color(0xFF73BD71, true));
            operationBut.setText("添加好友");
            if (Objects.equals(user.getId(), context.getCurrentUser().getId())) {
                operationBut.setEnabled(false);
            }
            operationBut.addActionListener(e -> {
                final RequestDialog dialog = new RequestDialog("请求添加好友");
                dialog.fillInData(user);
                dialog.setVisible(true);
            });
        }

        // 发送信息
        sendBut.addActionListener(e -> {
            final UserChatPanel panel = new UserChatPanel();
            panel.fillInData(user);
            sessionPanel.add(panel);
            GlobalContext.getContext().getListColumnPanel().getMessageListPanel().addItem(user, new Date());
        });
        this.repaint();
    }

    private JPanel createPanel(Dimension dimension) {
        final JPanel panel = new JPanel();
        // 边框
        final Border border = BorderFactory.createMatteBorder(1, 1, 1, 1,
                new Color(0xA0CECECE, true));
        panel.setBorder(border);
        SwingUtil.setSize(panel, dimension);
        return panel;
    }
}
